﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Timer
{
    class Program
    {
        static void Main(string[] args)
        {
            TimerThread t = new TimerThread();
            while (true)
            {
                Thread.Sleep(100000);
                Console.WriteLine("Main");
            }
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Console.WriteLine(e.Exception.Message);
            //Log = log4net.LogManager.GetLogger("");
            //string str = GetExceptionMsg(e.Exception, e.ToString());
            //MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //Log.Error(str);
            //LogManager.WriteLog(str);
        }
    }


    public class TimerThread
    {
        private System.Threading.Timer _timer = null;
        private int _count = 0;
        private readonly Object _lock = new Object();
       
        public TimerThread()
        {
            _timer = new System.Threading.Timer(new TimerCallback(TimerCb), null, 0, 100);
        }
        public void TimerCb(object obj)
        {
            lock (_lock)
            {
                _count++;
                int id = System.Threading.Thread.CurrentThread.ManagedThreadId;
                System.Threading.Thread.Sleep(1000);
                string msg = string.Format("线程ID：{0},计数{1} 当前时间:{2}", id, _count,DateTime.Now.ToString("yyyyMMdd HH:mm:ss"));
                Console.WriteLine(msg);
            }
        }
    }



}
